/*
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.webical.web.component.calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;
import org.webical.util.CalendarUtils;
import org.webical.web.action.IAction;
import org.webical.web.component.calendar.model.DatePickerModel;

/**
 * Repeater to repeat the calendar columns
 *
 * @author Mattijs Hoitink
 *
 */
public abstract class DatePickerColumnRepeater extends RepeatingView {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	protected static Class[] PANELACTIONS = new Class[] { };

	// CSS classes
	private static final String WEEK_NUMBER_CSS_CLASS = "datePickerWeekNumber";
	private static final String TODAY_CSS_CLASS = "calendarTodayItem";
	private static final String INRANGE_CSS_CLASS = "inRange";
	

	private DatePickerModel datePickerModel;
	private GregorianCalendar currentDate;
	private GregorianCalendar weekCalendar;

	public DatePickerColumnRepeater(String id, Calendar weekCalendar, DatePickerModel datePickerModel) {
		super(id);

		this.datePickerModel = datePickerModel;
		this.currentDate = new GregorianCalendar();
		this.currentDate.setTime(datePickerModel.getCurrentDate().getTime());
		this.weekCalendar = new GregorianCalendar();
		this.weekCalendar.setTime(weekCalendar.getTime());

		addColumns();
	}

	private void addColumns() {
		GregorianCalendar todayDate = new GregorianCalendar();
		Label weekLabel = new Label("week" + weekCalendar.get(Calendar.WEEK_OF_YEAR), String.valueOf(weekCalendar.get(Calendar.WEEK_OF_YEAR)));
		weekLabel.add(new AttributeAppender("class", true, new Model(WEEK_NUMBER_CSS_CLASS), " "));
		add(weekLabel);
		for(int i = 0; i < 7; i++) {

			DatePickerColumnPanel columnPanel = new DatePickerColumnPanel("day" + weekCalendar.get(Calendar.DAY_OF_YEAR), this.weekCalendar, datePickerModel) {
				private static final long serialVersionUID = 1L;

				@Override
				public void onAction(IAction action) {
					DatePickerColumnRepeater.this.onAction(action);
				}

			};
			// Check if date is in range
			if(
				(
					datePickerModel.getRangeStartDate() != null
					&&
					datePickerModel.getRangeEndDate() != null
				)
				&&
				(
					CalendarUtils.getStartOfDay(weekCalendar.getTime()).compareTo(CalendarUtils.getStartOfDay(datePickerModel.getRangeStartDate())) >= 0
					&& 
					CalendarUtils.getStartOfDay(weekCalendar.getTime()).compareTo(CalendarUtils.getStartOfDay(datePickerModel.getRangeEndDate())) <= 0
				)
			) {
				columnPanel.add(new AttributeAppender("class", true, new Model(INRANGE_CSS_CLASS), " "));
			}
			
			
			// Check if date is today
			if(CalendarUtils.getStartOfDay(weekCalendar.getTime()).equals(CalendarUtils.getStartOfDay(todayDate.getTime()))) {
				// Add css class
				columnPanel.add(new AttributeAppender("class", true, new Model(TODAY_CSS_CLASS), " "));
			}
			addOrReplace(columnPanel);

			// add a day to the week calendar
			this.weekCalendar.add(Calendar.DAY_OF_WEEK, 1);
			//weekCalendar.setTime(CalendarUtils.addDays(weekCalendar.getTime(), 1));
		}
	}

	/**
	 * Handle actions generated by this panel.
	 *
	 * @param action The action to handle
	 */
	public abstract void onAction(IAction action);

}